package com.xin.di.uav.applet.thread;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;

import java.util.Map;

@Slf4j
public class ContextDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        try {
            // Right now: Web thread context !
            // Grab the current thread MDC data
            Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
            return () -> {
                // Right now: @Async thread context !
                // Restore the Web thread context's MDC data
                log.info("------ decorate ------ ");
                if(copyOfContextMap != null){
                    MDC.setContextMap(copyOfContextMap);
                }
                runnable.run();
            };
        } finally {
//            MDC.clear();
        }
    }
}
